﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Principal;

namespace WebPattern.Core
{
    public class User : IPrincipal
    {
        #region Properties
        public virtual long Id { set; get; }
        public virtual string UserName { set; get; }
        public virtual UserType UserType { set; get; }// Descriminator
        public virtual string ApplicationName { set; get; }
        public virtual DateTime CreatedAt { set; get; }
        public virtual DateTime UpdatedAt { set; get; }
        public virtual string Name { set; get; }
        public virtual string Email { set; get; }
        public virtual string MobilePhone { set; get; }

        public virtual string Password { set; get; }
        public virtual string PasswordQuestion { set; get; }
        public virtual string PasswordAnswer { set; get; }
        public virtual DateTime? LastPasswordChangedDate { set; get; }
        public virtual bool IsApproved { set; get; }
        public virtual bool IsLockedOut { set; get; }
        public virtual bool IsOnline { set; get; }
        public virtual int FailedPasswordAttemptCount { set; get; }
        public virtual DateTime? FailedPasswordAttemptWindowStart { set; get; }
        public virtual int FailedPasswordAnswerAttemptCount { set; get; }
        public virtual DateTime? FailedPasswordAnswerAttemptWindowStart { set; get; }
        public virtual DateTime? LastLoginDate { set; get; }
        public virtual DateTime? LastActivityDate { set; get; }
        public virtual DateTime? LastLockoutDate { set; get; }
        public virtual DateTime? LastLogOutDate { set; get; }
        public virtual DateTime? PrevLoginDate { set; get; }
        public virtual string Comment { set; get; }
        public virtual bool Enabled { set; get; }

        public virtual ICollection<Role> UserRoles { set; get; }

        public virtual IIdentity Identity { set; get; }
        #endregion

        #region Constructors
        public User() { CreatedAt = DateTime.Now; }

        public User(string userName, string email)
        {
            this.CreatedAt = this.UpdatedAt = DateTime.Now;
            this.UserName = userName;
            this.Email = email;
        }

        public User(string email) : this(email, email) { }
        #endregion

        #region Overriden Methods
        public override string ToString() { return this.UserName; }
        #endregion

        #region Methods
        public virtual bool IsInRole(string role) { return UserRoles.Any(q => q.Name == role); }
        #endregion
    }
}
